# Create your views here.

import json

import ngender
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt
from gpt4all import GPT4All


@csrf_exempt
def guess_gender_by_name(request):
    req = json.loads(request.body.decode())
    flag_ = req['flag']
    res = ngender.guess(req['name'])
    if flag_ == 0 or flag_ == 1:
        res = res[flag_]
    else:
        res = ','.join(str(item) for item in res)
    return HttpResponse(res)


@csrf_exempt
def get_answer_form_model(request):
    req = json.loads(request.body.decode())
    model_name = req['model']
    max_tokens = req['max_tokens']
    question_text = req['question']

    model = GPT4All(model_name=model_name, model_path="/a_model", allow_download=False)
    output = model.generate(question_text, max_tokens=max_tokens)
    print(output)
    return HttpResponse(output)